package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tanhua.dubbo.api.UserInfoApi;

import com.tanhua.dubbo.api.mongo.MovementApi;
import com.tanhua.dubbo.api.mongo.VideoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVoAge;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    /**
     * 分页查询用户信息
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult findPage(Long page, Long pageSize) {
        // 分页查询用户信息
        PageResult pageResult = userInfoApi.findPage(page,pageSize);
        // 获取分页结果集
        List<UserInfo> userInfoList = pageResult.getItems();
        // 转成vo
        List<UserInfoVoAge> voList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(userInfoList)){
            for (UserInfo userInfo : userInfoList) {
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(userInfo, vo);
                voList.add(vo);
            }
        }
        // 设置
        pageResult.setItems(voList);
        return pageResult;
    }

    /**
     * 查询用户详情
     * @param userId
     * @return
     */
    public UserInfoVoAge findById(Long userId) {
        UserInfo userInfo = userInfoApi.findUserInfo(userId);
        UserInfoVoAge vo = new UserInfoVoAge();
        BeanUtils.copyProperties(userInfo, vo);
        // 前端时间是一个integer, 是一个时间戳
        vo.setCreated(userInfo.getCreated().getTime());
        return vo;
    }



    //查询指定用户发布的所有视频列表
    public PageResult findAllVideos(Long page, Long pagesize, Long uid) {
        return videoApi.findByUserId(page, pagesize, uid);
    }


    //查询动态
    public PageResult findAllMovements(Long page, Long pagesize, Long uid, Integer state) {
        //1、调用API查询数据 ：Movment对象
        PageResult result = movementApi.findByUserId(uid,state,page,pagesize);
        //2、解析PageResult，获取Movment对象列表
        List<Movement> items = (List<Movement>) result.getItems();
        //3、一个Movment对象转化为一个Vo
        if(CollUtil.isEmpty(items)) {
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByCondition(userIds, null);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = map.get(movement.getUserId());
            if(userInfo != null) {
                MovementsVo vo = MovementsVo.init(userInfo, movement);
                vos.add(vo);
            }
        }
        //4、构造返回值
        result.setItems(vos);
        return result;
    }



}
